// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-6-26

#ifndef __LISTVIEW_H_COMMON_UI__
#define __LISTVIEW_H_COMMON_UI__

#pragma once

#include "wnd.h"
#include "scrollbar.h"

class CJoyListView;

//##ModelId=44C4B603039A
enum JoyListViewSortType
{
	//##ModelId=44C4B60303AA
    LVS_None,
	//##ModelId=44C4B60303AB
    LVS_Desc,
	//##ModelId=44C4B60303AC
    LVS_Asce,
};

//##ModelId=44C4B60303C8
class CJoyListViewItem : public CJoyCtrl
{
protected:
    friend class CJoyListView;

	//##ModelId=44C4B60303DA
    CJoyListView*   m_listView;
	//##ModelId=44C4B60303DF
    POSITION        m_selPos;

	//##ModelId=44C4B6040000
    virtual void CM_Mouse(MouseMessage msg, const CPoint& pt);
	//##ModelId=44C4B604000F
    virtual void CM_Draw(Graphics& graph);
	//##ModelId=44C4B604001F
    virtual void CM_SelItem(POSITION selPos);
public:
	//##ModelId=44C4B604003E
    CJoyListViewItem();
	//##ModelId=44C4B604004E
    virtual ~CJoyListViewItem();

	//##ModelId=44C4B604005D
    bool IsSel()
    {
        return m_selPos != NULL;
    }
};

//##ModelId=44C4B604007D
class CJoyListViewPanel : public CJoyCtrl
{
protected:
    friend class CJoyListView;

	//##ModelId=44C4B6040080
    CJoyListView*   m_listView;

	//##ModelId=44C4B6040084
    virtual void CM_Draw(Graphics& graph);
	//##ModelId=44C4B604008E
    virtual void CM_Size(const CSize& s);
	//##ModelId=44C4B6040091
    virtual void CM_Mouse(MouseMessage msg, const CPoint& pt);
public:
	//##ModelId=44C4B604009F
    CJoyListViewPanel();
	//##ModelId=44C4B60400A0
    virtual ~CJoyListViewPanel();

public:
    //afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
    //afx_msg void OnSetFocus(CWnd* pOldWnd);
    //afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
};

//##ModelId=44C4B60400AB
class CJoyListViewHeader : public CJoyCtrl
{
protected:
    friend class CJoyListView;

	//##ModelId=44C4B60400BD
    CJoyListView*   m_listView;

	//##ModelId=44C4B60400C1
    virtual void CM_Size(const CSize& s);
public:
	//##ModelId=44C4B60400CC
    CJoyListViewHeader();
	//##ModelId=44C4B60400CD
    virtual ~CJoyListViewHeader();

};

//##ModelId=44C4B60400EA
class CJoyListViewField : public CJoyCtrl
{
protected:
	//##ModelId=44C4B60400EC
    virtual void CM_Draw(Graphics& graph);

	//##ModelId=44C4B6040109
    virtual void CM_Sort();
	//##ModelId=44C4B6040119
    virtual void CM_LoadSkin(CJoyXmlElement* pSkinElem);
public:
	//##ModelId=44C4B6040129
    CJoyListView*   m_listView;
	//##ModelId=44C4B604013B
    CStringW    m_title;
	//##ModelId=44C4B604013F
    int         m_min;
	//##ModelId=44C4B6040140
    int         m_max;
	//##ModelId=44C4B6040148
    float       m_percent;
	//##ModelId=44C4B6040149
    CRect       m_itemArea;
	//##ModelId=44C4B604014A
    bool        m_sorted;
	//##ModelId=44C4B6040158
    JoyListViewSortType m_sortType;

	//##ModelId=44C4B604015C
    virtual void CM_Mouse(MouseMessage msg, const CPoint& pt);
	//##ModelId=44C4B6040177
    virtual int CM_CompareItem(CJoyListViewItem*, CJoyListViewItem*);
public:
	//##ModelId=44C4B6040186
    CJoyListViewField();

	//##ModelId=44C4B60401A5
    void MakeAsLastScale();
	//##ModelId=44C4B60401B5
    virtual void ResetItemArea();
};

class CJoyListViewHBarWrapper;

//##ModelId=44C4B6040251
class CJoyListView :
    public CJoyWnd
{
	//Adds the ability to access run-time information about an object's class when deriving a class from CObject.
	DECLARE_DYNAMIC(CJoyListView)   
public:
	//##ModelId=44C4B60500DD
    typedef void (CJoyWnd::*PSelItem)(CJoyListViewItem* item);
protected:

    friend class CJoyListViewItem;
    friend class CJoyListViewPanel;
    friend class CJoyListViewField;
    friend class CJoyListViewHeader;
    friend class CJoyListViewHBarWrapper;

	//##ModelId=44C4B6040261
    bool m_multiSel;
	//##ModelId=44C4B6040262
    bool m_scaleWidth;


	//##ModelId=44C4B6040263
    int m_itemHeight;
	//##ModelId=44C4B6040271
    int m_itemWidth;
	//##ModelId=44C4B6040272
    int m_leftVisible;

	//##ModelId=44C4B6040274
    Color   m_frameColor;

	//##ModelId=44C4B6040283
    CJoyScrollBar*  m_vBar;
	//##ModelId=44C4B6040288
    CJoyScrollBar*  m_hBar;
	//##ModelId=44C4B6040291
    CJoyListViewHBarWrapper*    m_hBarWrapper;
	//##ModelId=44C4B6040296
    CJoyListViewHeader* m_header;

	//##ModelId=44C4B60402A3
    CList<CJoyListViewItem*>    m_selItems;

	//##ModelId=44C4B60402B0
    CJoyWnd*    m_wndSelItem;
	//##ModelId=44C4B60402B5
    PSelItem    m_onSelItem;

	//##ModelId=44C4B60402BF
    virtual CJoyListViewHeader* CreateHeader();
	//##ModelId=44C4B60402C1
    virtual CJoyListViewField* CreateField();
	//##ModelId=44C4B60402CF
    virtual CJoyListViewPanel* CreatePanel();
	//##ModelId=44C4B60402DE
    virtual CJoyListViewItem* CreateItem();
    template<typename FieldType>
    void AddField_T(FieldType*& f, PCSTR name)
    {
        f = new FieldType();
        f->m_name = name;
        AddFieldTail(f);
    }

    
	//##ModelId=44C4B60402E0
    virtual void CE_VScroll(CJoyScrollBar*, CJoyScrollBar::SCROLLBAR_MESSAGE, float&);
	//##ModelId=44C4B60402F0
    virtual void CE_HScroll(CJoyScrollBar*, CJoyScrollBar::SCROLLBAR_MESSAGE, float&);
	//##ModelId=44C4B6040300
    virtual void CM_LoadSkin(CJoyXmlElement* pSkinElem);
	//##ModelId=44C4B604030F
    virtual void CM_Draw(Graphics& graph);
	//##ModelId=44C4B604031C
    virtual void CM_SelItem(CJoyListViewItem* item);
public:
	//##ModelId=44C4B604031F
    int m_firstVisible;
	//##ModelId=44C4B604032C
    int m_firstVisibleTop;
	//##ModelId=44C4B604032E
    CList<CJoyListViewField*>   m_fields;
	//##ModelId=44C4B604033D
    CJoyListViewPanel*  m_itemPanel;
	//##ModelId=44C4B6040341
    int m_hScrollStep;
	//##ModelId=44C4B604034C
    CList<CJoyListViewItem*>    m_items;
public:
	//##ModelId=44C4B6040350
    CJoyListView(void);
	//##ModelId=44C4B604035B
    virtual ~CJoyListView(void);

	//##ModelId=44C4B604037A
    int GetVisibleItemCount() const
    {
        return (m_itemPanel->GetArea().Height())/m_itemHeight;
    }
	//##ModelId=44C4B604037C
    int GetTotalVisibleItemCount() const;
	//##ModelId=44C4B604037E
    int GetHeaderWidth() const;

	//##ModelId=44C4B6040380
    void AddItemTail(CJoyListViewItem* item, bool rebuildArea = true);
	//##ModelId=44C4B604038C
    void AddItemHead(CJoyListViewItem* item, bool rebuildArea = true);
	//##ModelId=44C4B604039B
    void RemoveItem(CJoyListViewItem* item);
	//##ModelId=44C4B60403AA
    void RemoveAll();

	//##ModelId=44C4B60403B9
    void AddFieldTail(CJoyListViewField* field, bool rebuildArea = true);
	//##ModelId=44C4B60403BC
    void AddFieldHead(CJoyListViewField* field, bool rebuildArea = true);
	//##ModelId=44C4B60403CA
    void ShowHeader(bool show);

	//##ModelId=44C4B60403D8
    void BuildItemArea();
	//##ModelId=44C4B60403D9
    void BuildFieldArea();

	//##ModelId=44C4B6050000
    void SelItem(CJoyListViewItem* item);
	//##ModelId=44C4B605000F
    CJoyListViewItem* SelItem(int idx);
	//##ModelId=44C4B605001F
    void UnselItem(CJoyListViewItem* item);
	//##ModelId=44C4B6050021
    void UnselAllItem();
	//##ModelId=44C4B605002E
    int GetItemIndex(CJoyListViewItem* item);

	//##ModelId=44C4B605003E
    void ScrollTo(int firstVisible);
	//##ModelId=44C4B605004E
    void ScrollOffset(int step);
	//##ModelId=44C4B605005D
    void SetSelItemEvent(CJoyWnd* pWnd, PSelItem pOnSel);

	//##ModelId=44C4B605006D
    virtual void SortItem(CJoyListViewField* pField, JoyListViewSortType sortType);
	//##ModelId=44C4B605008C
    void SortItem(CJoyListViewField* pField, JoyListViewSortType sortType, POSITION startPos, POSITION endPos, bool buildArea = true);

	//##ModelId=44C4B60500AB
    CJoyListViewPanel* GetPanel()
    {
        return m_itemPanel;
    }
	//##ModelId=44C4B60500AC
    CJoyListViewField* GetField(int i);
	//##ModelId=44C4B60500BB
    CJoyListViewField* GetField(PCSTR pszName);
public:
    DECLARE_MESSAGE_MAP()
	//##ModelId=44C4B60500DA
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};


#endif  // __LISTVIEW_H_COMMON_UI__
